"use strict";

/**
 * @name /
 * @param int id 活动ID
 * @param int ik 奖励序号
 * 活动配置
 */

module.exports = function () {
    let id = this.get('id', 'string');
    let ik = this.get('ik', "int");
    if (!id) {
        return this.error("id empty");
    }
    let keys = this.get('keys', "array");

    let fields = ["_id", "sid", "cid", "TType", "STime", "ETime", "status"];
    if (ik) {
        fields.push(["item", ik].join("."));
    }

    if (Array.isArray(keys)) {
        fields = fields.concat(keys)
    }
    let configModel = this.model.branch("config", this.branch);
    return configModel.get(id, fields);
};


module.exports.getAllByType = function () {
    let query = this.get('query', 'json');
    let ik = this.get('ik', "int");
    if (!query) {
        return this.error("query empty");
    }
    let keys = this.get('keys', "array");

    //TODO: 客户端说不要sid "sid", 
    let fields = ["_id", "sid", "cid", "TType", "STime", "ETime", "status"];
    if (ik) {
        fields.push(["item", ik].join("."));
    }

    if (Array.isArray(keys)) {
        fields = fields.concat(keys)
    }

    let configModel = this.model.branch("config", this.branch);
    let option = { multi: true, dataType: "array", fields: fields };
    return configModel.find(query, option);
    //return configModel.get(id,fields);
};


module.exports.getSupActive = function () {
    let s_type = this.get('s_type', 'string');
    let fields = ["_id", "s_id", "s_arg1", "s_arg2", "STime", "ETime", "status"];
    let configModel = this.model.branch("config", this.branch);
    return configModel.find({ 's_type': s_type, 'status': { '$ne': 2 } }, fields);
};
